﻿using CloudFranceTriple.Models;
using System.Linq;
using System.Web.Mvc;
using System.Web.Security;
using WebMatrix.WebData;

namespace CloudFranceTriple.WebRole.Areas.UserManager.Controllers
{
    public class RoleUsersController : Controller
    {
        private cloudfrAboq2RsDMEntities db = new cloudfrAboq2RsDMEntities();

        private readonly IWebSecurityWrapper _webSecurity;
        public RoleUsersController()
        {
            this._webSecurity = new WebSecurityWrapper();
        }

        public RoleUsersController(IWebSecurityWrapper webSecurity)
        {
            this._webSecurity = webSecurity;
        }
        //
        // GET: /UserManager/RoleUsers/RoleId

        [Authorize(Roles = "RoleHR")]
        public ActionResult Index(int id = 0)
        {
            var role = db.webpages_Roles.Find(id);
            if (role == null)
            {
                return HttpNotFound();
            }

            ViewBag.RoleId = id;
            ViewBag.RoleName = role.RoleName;

            var roleUsers = System.Web.Security.Roles.GetUsersInRole(role.RoleName);
            var result = db.UserProfile.Where(user1 => roleUsers.Contains(user1.UserName)).OrderBy(user2 => user2.UserName);

            var UnlinkedUser = db.UserProfile.Except(result).OrderBy(user3 => user3.UserName);

            ViewBag.User_Id = new SelectList(UnlinkedUser, "UserId", "UserName");
            ViewBag.ShowLinkUser = (UnlinkedUser.ToList().Count() != 0);

            var selectedUserId = Request.Form["User_Id"];
            if (selectedUserId != null)
            {
                return RedirectToAction("Link", "RoleUsers", new { id = int.Parse(selectedUserId.ToString()), id2 = id, area = "UserManager" });
            }

            var userTimeZoneInfo = Extension.GetUserTimeZoneInfo(_webSecurity.CurrentUserId);
            foreach (UserProfile user in result)
            {
                user.LastRequestDate = user.LastRequestDate.ToUserTimeZone(userTimeZoneInfo);
            }

            return View(result.ToList());
        }

        //
        // GET: /UserManager/RoleUsers/Link/UserId/RoleId

        [Authorize(Roles = "RoleHR")]
        public ActionResult Link(int id = 0, int id2 = 0)
        {
            var user = db.UserProfile.Find(id);
            var role = db.webpages_Roles.Find(id2);
            if (user == null || role == null)
            {
                return HttpNotFound();
            }
            Roles.AddUserToRole(user.UserName, role.RoleName);
            return RedirectToAction("Index", "RoleUsers", new { id = id2, area = "UserManager" });
        }

        //
        // GET: /UserManager/RoleUsers/Unlink/UserId/RoleId

        [Authorize(Roles = "RoleHR")]
        public ActionResult Unlink(int id = 0, int id2 = 0)
        {
            var user = db.UserProfile.Find(id);
            var role = db.webpages_Roles.Find(id2);
            if (user == null || role == null)
            {
                return HttpNotFound();
            }
            Roles.RemoveUserFromRole(user.UserName, role.RoleName);
            return RedirectToAction("Index", "RoleUsers", new { id = id2, area = "UserManager" });
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}